import request from '@/utils/requestLogin'
import type { Article } from '@/types/article'

export interface ArticleQuery {
    page: number
    pageSize: number
    title?: string
    category?: string
    status?: string
}

export interface ArticleListResponse {
    list: Article[]
    total: number
}

export interface LifeArticleResponse {
    code: number
    data: {
        list: Article[]
        total: number
    }
}

export interface LifeCategoriesResponse {
    code: number
    data: string[]
}

// 将所有API方法导出为命名导出
export const articleApi = {
    // 获取文章列表
    getList(params: ArticleQuery) {
        return request.get<{ code: number, data: ArticleListResponse }>('http://cylyxsz.w1.luyouxia.net/blog/api/articles', params)
    },

    // 获取文章详情
    getDetail(id: number) {
        return request.get<{ code: number, data: Article }>(`http://cylyxsz.w1.luyouxia.net/blog/api/admin/articles/${id}`)
    },

    // 创建文章
    create(data: Partial<Article>) {
        return request.post<{ code: number, data: Article }>('http://cylyxsz.w1.luyouxia.net/blog/api/articles', data)
    },

    // 更新文章
    update(id: number, data: Partial<Article>) {
        return request.put<{ code: number, data: Article }>(`http://cylyxsz.w1.luyouxia.net/blog/api/articles/${id}`, data)
    },

    // 删除文章
    delete(id: number) {
        return request.delete<{ code: number }>(`http://cylyxsz.w1.luyouxia.net/blog/api/articles/${id}`)
    },

    // 获取上一篇文章
    getPrevArticle(id: number) {
        return request.get<{ code: number, data: Article }>(`http://cylyxsz.w1.luyouxia.net/blog/api/articles/${id}/prev`)
    },

    // 获取下一篇文章
    getNextArticle(id: number) {
        return request.get<{ code: number, data: Article }>(`http://cylyxsz.w1.luyouxia.net/blog/api/articles/${id}/next`)
    },

    // 获取生活博客文章列表
    getLifeArticles(params: {
        page: number;
        pageSize: number;
        category?: string;
        search?: string;
    }) {
        return request.get<LifeArticleResponse>('http://cylyxsz.w1.luyouxia.net/blog/api/articles/life', params)
    },

    // 获取生活博客文章分类
    getLifeCategories() {
        return request.get<LifeCategoriesResponse>('http://cylyxsz.w1.luyouxia.net/blog/api/articles/life/categories')
    },

    // 获取生活博客文章详情
    getLifeArticleDetail(id: number, password?: string) {
        const url = `http://cylyxsz.w1.luyouxia.net/blog/api/articles/life/${id}`
        // 如果有密码，直接构造带密码的 URL
        if (password) {
            return request.get<{ code: number, data: Article }>(`${url}?password=${password}`)
        }
        // 没有密码就正常请求
        return request.get<{ code: number, data: Article }>(url)
    }
}

// 为了向后兼容，保留默认导出
export default articleApi 